
package org.wxy.sdk.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.Map;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by wxy on 2018/05/27.
 *
 * @author wxy
 * @date on 2018/05/27 11:32
 */
public class PreferenceUtils {
    public static String getPrefString(Context context, String preference, String key, final String defaultValue) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        return settings.getString(key, defaultValue);
    }

    public static void setPrefString(Context context, String preference, final String key, final String value) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        settings.edit().putString(key, value).commit();
    }

    public static boolean getPrefBoolean(Context context, String preference, final String key,
                                         final boolean defaultValue) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        return settings.getBoolean(key, defaultValue);
    }

    public static boolean hasKey(Context context, String preference, final String key) {
        return context.getSharedPreferences(preference, MODE_PRIVATE).contains(key);
    }

    public static void setPrefBoolean(Context context, String preference, final String key, final boolean value) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        settings.edit().putBoolean(key, value).commit();
    }

    public static void setPrefInt(Context context, String preference, final String key, final int value) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        settings.edit().putInt(key, value).commit();
    }

    public static int getPrefInt(Context context, String preference, final String key, final int defaultValue) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        return settings.getInt(key, defaultValue);
    }

    public static void setPrefFloat(Context context, String preference, final String key, final float value) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        settings.edit().putFloat(key, value).commit();
    }

    public static float getPrefFloat(Context context, String preference, final String key, final float defaultValue) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        return settings.getFloat(key, defaultValue);
    }

    public static void setSettingLong(Context context, String preference, final String key, final long value) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        settings.edit().putLong(key, value).commit();
    }

    public static long getPrefLong(Context context, String preference, final String key, final long defaultValue) {
        final SharedPreferences settings = context.getSharedPreferences(preference, MODE_PRIVATE);
        return settings.getLong(key, defaultValue);
    }

    public static void clearPreference(Context context, final SharedPreferences p) {
        final Editor editor = p.edit();
        editor.clear();
        editor.commit();
    }
    public static void removeString(Context context, String preference, String key) {
    	SharedPreferences sp = context.getSharedPreferences(preference, MODE_PRIVATE);
    	Editor et = sp.edit();
    	et.remove(key).commit();
    }
    
    public static Map<String,?> getAll(Context context, String preference){
    	return context.getSharedPreferences(preference, MODE_PRIVATE).getAll();
    }
}
